

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head><!-- hexo injector head_begin start --><link rel="stylesheet" href="/css/my-avatar.css"><!-- hexo injector head_begin end -->
  <meta charset="UTF-8">

  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/icon/coffee.png">
  

  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="晓寒">
  <meta name="keywords" content="">
  
    <meta name="description" content="排序算法的解析和实践">
<meta property="og:type" content="article">
<meta property="og:title" content="排序算法">
<meta property="og:url" content="http://example.com/posts/61237/index.html">
<meta property="og:site_name" content="Xiaohan&#96;s Blog">
<meta property="og:description" content="排序算法的解析和实践">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-11-12T01:39:16.000Z">
<meta property="article:modified_time" content="2025-03-17T15:16:22.917Z">
<meta property="article:author" content="晓寒">
<meta property="article:tag" content="算法学习">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>排序算法 - Xiaohan`s Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1749284_5i9bdhy70f8.css">



<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1736178_k526ubmyhba.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/css/mac.css">
<link rel="stylesheet" href="/css/shubiao.css">
<link rel="stylesheet" href="/css/huadong.css">
<link rel="stylesheet" href="/css/fonts.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.8","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":true,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false},"umami":{"src":null,"website_id":null,"domains":null,"start_time":"2024-01-01T00:00:00.000Z","token":null,"api_server":null}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"></head>


<body><!-- hexo injector body_begin start --><div id="web_bg"></div><!-- hexo injector body_begin end -->
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Xiaohan`s blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>Home</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>Category</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>Tag</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>Archive</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/" target="_self">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>About</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/bg/mountains.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="排序算法"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-11-12 09:39" pubdate>
          2022年11月12日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          3.4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          29 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">排序算法</h1>
            
              <p id="updated-time" class="note note-info" style="">
                
                  
                    本文最后更新于 2025年3月17日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <div class="note note-warning">
            <p><strong>Warning</strong></p><p><strong>此处不对排序的空间和时间复杂度进行分析，关注排序原理和代码实现。</strong></p>
          </div>
<h1 id="排序算法">排序算法</h1>
<h2 id="冒泡排序选择排序插入排序">冒泡排序、选择排序、插入排序</h2>
<p>这几种排序是较为基础且容易理解的所以就直接放代码了。</p>
<h3 id="冒泡排序">冒泡排序</h3>
<p>代码如下： <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;time.h&gt;</span></span><br><span class="hljs-type">int</span> a[<span class="hljs-number">1000001</span>];<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span>;<span class="hljs-comment">//随机数</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">bubble_sort</span><span class="hljs-params">(<span class="hljs-type">int</span> q)</span>;<br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span><br>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;参与排序的随机数个数：&quot;</span>);<br>    <span class="hljs-type">int</span> n;<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;n);<br>    random(n);<br>    <span class="hljs-type">double</span> start, finish;<br>    start = (<span class="hljs-type">double</span>)clock();<br>    bubble_sort(n);<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++)<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>, a[i]);<br>    finish = (<span class="hljs-type">double</span>)clock();<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%lf ms&quot;</span>, finish - start);<span class="hljs-comment">//运行时间</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">bubble_sort</span><span class="hljs-params">(<span class="hljs-type">int</span> q)</span><br>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt; q; i++) &#123;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> j = q - <span class="hljs-number">1</span>; j &gt;= i; j--) &#123;<br>            <span class="hljs-keyword">if</span> (a[j + <span class="hljs-number">1</span>] &lt; a[j]) &#123;<br>                <span class="hljs-type">int</span> tmp = a[j + <span class="hljs-number">1</span>]; a[j + <span class="hljs-number">1</span>] = a[j]; a[j] = tmp;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span><br>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) &#123;<br>        srand(i);<br>        a[i] = rand() % <span class="hljs-number">100</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure></p>
<h3 id="选择排序">选择排序</h3>
<p>代码如下： <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;time.h&gt;</span></span><br><span class="hljs-type">int</span> a[<span class="hljs-number">1000001</span>];<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span>;<span class="hljs-comment">//随机数</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">selection_sort</span><span class="hljs-params">(<span class="hljs-type">int</span> q)</span>;<br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span><br>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;参与排序的随机数个数：&quot;</span>);<br>    <span class="hljs-type">int</span> n;<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;n);<br>    random(n);<br>    <span class="hljs-type">double</span> start, finish;<br>    start = (<span class="hljs-type">double</span>)clock();<br>    selection_sort(n);<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++)<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>, a[i]);<br>    finish = (<span class="hljs-type">double</span>)clock();<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%lf ms&quot;</span>, finish - start);<span class="hljs-comment">//运行时间</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">selection_sort</span><span class="hljs-params">(<span class="hljs-type">int</span> q)</span><br>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt; q; i++) &#123;<br>        <span class="hljs-type">int</span> k = i;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> j = i + <span class="hljs-number">1</span>; j &lt;= q; j++) &#123;<br>            <span class="hljs-keyword">if</span> (a[k] &gt; a[j]) &#123;<br>                k = j;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">if</span> (k != i) &#123;<br>            <span class="hljs-type">int</span> tmp = a[k]; a[k] = a[i]; a[i] = tmp;<br>        &#125;<br>    &#125;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span><br>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) &#123;<br>        srand(i);<br>        a[i] = rand() % <span class="hljs-number">100</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure></p>
<h3 id="插入排序">插入排序</h3>
<p>代码如下： <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;time.h&gt;</span></span><br><span class="hljs-type">int</span> a[<span class="hljs-number">1000001</span>];<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span>;<span class="hljs-comment">//随机数</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">insertion_sort</span><span class="hljs-params">(<span class="hljs-type">int</span> q)</span>;<br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span><br>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;参与排序的随机数个数：&quot;</span>);<br>    <span class="hljs-type">int</span> n;<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;n);<br>    <span class="hljs-type">double</span> start, finish;<br>    start = (<span class="hljs-type">double</span>)clock();<br>    random(n);<br>    insertion_sort(n);<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++)<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>, a[i]);<br>	    finish = (<span class="hljs-type">double</span>)clock();<br>	    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%lf ms&quot;</span>, finish - start);<span class="hljs-comment">//运行时间</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">insertion_sort</span><span class="hljs-params">(<span class="hljs-type">int</span> q)</span><br>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">2</span>; i &lt;= q; i++) &#123;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> j = i - <span class="hljs-number">1</span>; j &gt;= <span class="hljs-number">1</span>; j--) &#123;<br>            <span class="hljs-keyword">if</span> (a[j] &lt;= a[i]) &#123;<br>                <span class="hljs-type">int</span> tmp = a[i];<span class="hljs-comment">//插入</span><br>                <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> k = i - <span class="hljs-number">1</span>; k &gt; j; k--) &#123;<br>                    a[k + <span class="hljs-number">1</span>] = a[k];<br>                &#125;<br>                a[j + <span class="hljs-number">1</span>] = tmp;<br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span><br>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) &#123;<br>        srand(i);<br>        a[i] = rand() % <span class="hljs-number">100</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure></p>
<h2 id="桶排序">桶排序</h2>
<p>原理：对于确定范围的若干个数排序，先放上包括所有数据范围的桶，把数据放到对应编号的桶中，输出按照桶的编号从小到大输出。</p>
<p>假设数据范围为0~8：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="数据" />
<figcaption aria-hidden="true">数据</figcaption>
</figure>
<p>将数据全部放入桶中：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="桶排序" />
<figcaption aria-hidden="true">桶排序</figcaption>
</figure>
<p>结果：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="结果" />
<figcaption aria-hidden="true">结果</figcaption>
</figure>
<p>代码如下： <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;time.h&gt;</span></span><br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span>;<span class="hljs-comment">//随机数</span><br><span class="hljs-type">int</span> a[<span class="hljs-number">10000004</span>], b[<span class="hljs-number">10000004</span>];<br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span><br>&#123;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;参与排序的随机数个数：&quot;</span>);<br>    <span class="hljs-type">int</span> n;<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;n);<br>    <span class="hljs-type">double</span> start, finish;<br>    start = (<span class="hljs-type">double</span>)clock();<br>    random(n);<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++)b[a[i]]++;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">10000003</span>; i++) &#123;<br>        <span class="hljs-keyword">while</span> (b[i] != <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>, i);<br>            b[i]--;<br>        &#125;<br>    &#125;<br>    finish = (<span class="hljs-type">double</span>)clock();<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%lf ms&quot;</span>, finish - start);<span class="hljs-comment">//运行时间</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span><br>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) &#123;<br>        a[i] = rand() % <span class="hljs-number">10000000</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure> ## 基数排序</p>
<p>原理：对于组数，按照位数从右向左对每个数进行排序。由于单个数字只有0~9故使用桶排序来提高效率。</p>
<p>假设有如下数据：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="原数据" />
<figcaption aria-hidden="true">原数据</figcaption>
</figure>
<p>第一次操作：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="第一次操作" />
<figcaption aria-hidden="true">第一次操作</figcaption>
</figure>
<p>第二次操作：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="第二次操作" />
<figcaption aria-hidden="true">第二次操作</figcaption>
</figure>
<p><span class="math display">\[
\dots
\]</span> 最后的结果：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="结果" />
<figcaption aria-hidden="true">结果</figcaption>
</figure>
<p>代码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;string.h&gt;</span>  </span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;time.h&gt;</span></span><br><span class="hljs-type">int</span> p[<span class="hljs-number">10000004</span>],q[<span class="hljs-number">10000003</span>];<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span><br>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) &#123;<br>        srand(i);<br>        p[i<span class="hljs-number">-1</span>] = rand() % <span class="hljs-number">100</span>;<br>    &#125;<br>&#125;<br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span><br>&#123;<br>    <span class="hljs-type">int</span> n;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;参与排序的个数：&quot;</span>);<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;n);<br>    random(n);<br>    <span class="hljs-type">double</span> start, finish;<br>    start = (<span class="hljs-type">double</span>)clock();<br>    <span class="hljs-comment">//动态分配内存   </span><br>    <span class="hljs-type">int</span> max = p[<span class="hljs-number">0</span>], d = <span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) &#123;<br>        <span class="hljs-keyword">if</span> (p[i - <span class="hljs-number">1</span>] &gt; max)max = p[i - <span class="hljs-number">1</span>];<br>    &#125;<span class="hljs-comment">//求最大值  </span><br>    <span class="hljs-keyword">while</span> (max &gt; <span class="hljs-number">0</span>) &#123;<br>        d++;<br>        max /= <span class="hljs-number">10</span>;<br>    &#125;<span class="hljs-comment">//求最大位数  </span><br>    <span class="hljs-type">int</span> radx = <span class="hljs-number">1</span>;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt; d; i++) &#123;<br>        <span class="hljs-type">int</span> a[<span class="hljs-number">11</span>] = &#123; <span class="hljs-number">0</span> &#125;;<span class="hljs-comment">//初始化，桶排序 </span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> t = <span class="hljs-number">0</span>; t &lt; n; t++) &#123;<br>            a[p[t] / radx % <span class="hljs-number">10</span>+<span class="hljs-number">1</span>]++;<span class="hljs-comment">//记录个数</span><br>        &#125;<br>        <span class="hljs-comment">//标序 </span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> t = <span class="hljs-number">1</span>; t &lt; <span class="hljs-number">10</span>; t++) &#123;<br>            a[t] = a[t] + a[t - <span class="hljs-number">1</span>];<span class="hljs-comment">//记录开始的位置</span><br>        &#125;<br>        <span class="hljs-comment">//排序</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> j = <span class="hljs-number">0</span>; j &lt; n; j++) &#123;<br>            <span class="hljs-type">int</span> k = (p[j] / radx) % <span class="hljs-number">10</span>;<br>            q[a[k]] = p[j];<br>            a[k]++;<br>        &#125;<br>        <span class="hljs-comment">//复制 </span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> j = <span class="hljs-number">0</span>; j &lt; n; j++) &#123;<br>            p[j] = q[j];<br>        &#125;<br>        radx *= <span class="hljs-number">10</span>;<br>    &#125;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++) &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>, p[i]);<br>    &#125;<br>    finish = (<span class="hljs-type">double</span>)clock();<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%lf ms&quot;</span>, finish - start);<span class="hljs-comment">//运行时间</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>由于内存占用大，关键字值域小时可以采用</p>
<hr />
<h2 id="快速排序">快速排序</h2>
<p>单次操作：选择一个参照的数据（一般是第一个数），下标 i , j
分别从数组的头和尾开始，i , j 相向遍历，当下标 i
所指的数大于参照数据，下标 j
所指的数据小于参照数据时，两个数据相互换位置。
并且当i=j时，参照数据位置和 i/j 指向的数据交换位置，结束本次操作。</p>
<p>单次结果：参照数据被排在正确的位置，将原数组分为左右两个区间。</p>
<p>整体：通过递归使各个数据归位，完成排序。</p>
<p>这里提供作者 大卫·加勒斯 的quick sort<a
target="_blank" rel="noopener" href="https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html">动态图地址</a>。</p>
<p>代码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-type">void</span> <span class="hljs-title function_">quicksort</span><span class="hljs-params">(<span class="hljs-type">int</span> head,<span class="hljs-type">int</span> tail,<span class="hljs-type">int</span>* p)</span>;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> n,a[<span class="hljs-number">1000</span>];  <br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;n);  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;i&lt;n;i++)&#123;  <br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;a[i]);  <br>    &#125;  <br>    quicksort(<span class="hljs-number">0</span>,n<span class="hljs-number">-1</span>,a);  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;i&lt;n;i++)&#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>,a[i]);  <br>    &#125;  <br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">quicksort</span><span class="hljs-params">(<span class="hljs-type">int</span> head,<span class="hljs-type">int</span> tail,<span class="hljs-type">int</span>*p)</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> i=head,j=tail;  <br>    <span class="hljs-type">int</span> tmp=p[head];  <br>    <span class="hljs-keyword">if</span>(i&gt;j)&#123;  <br>        <span class="hljs-keyword">return</span>;<span class="hljs-comment">//函数递归结束  </span><br>    &#125;  <br>    <span class="hljs-keyword">while</span>(i!=j)&#123;  <br>        <span class="hljs-keyword">while</span>(p[j]&gt;=tmp&amp;&amp;j&gt;i)&#123;  <br>            j--;  <br>        &#125;  <br>        <span class="hljs-keyword">while</span>(p[i]&lt;=tmp&amp;&amp;j&gt;i)&#123;  <br>            i++;  <br>        &#125;  <br>        <span class="hljs-keyword">if</span>(j&gt;i)&#123;  <br>            <span class="hljs-type">int</span> temp=p[j];p[j]=p[i];p[i]=temp;  <br>        &#125;  <br>    &#125;  <br>    p[head]=p[i];p[i]=tmp;  <br>    quicksort(head,i<span class="hljs-number">-1</span>,p); <span class="hljs-comment">//左区间递归 </span><br>    quicksort(i+<span class="hljs-number">1</span>,tail,p);  <span class="hljs-comment">//右区间递归</span><br>    <span class="hljs-keyword">return</span>;  <br>&#125;<br></code></pre></td></tr></table></figure>
<p>同时c语言中也提供qsort()函数，在&lt;stdlib.h&gt;中，使用方法如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdlib.h&gt;</span>  </span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-type">int</span> <span class="hljs-title function_">cmp</span><span class="hljs-params">(<span class="hljs-type">void</span>* p1,<span class="hljs-type">void</span>*p2)</span>;  <br><span class="hljs-comment">//如果compar返回值&lt; 0，那么p1所指向元素会被排在p2所指向元素的前面  </span><br><span class="hljs-comment">//如果compar返回值= 0，那么p1所指向元素与p2所指向元素的顺序不确定  </span><br><span class="hljs-comment">//如果compar返回值&gt; 0，那么p1所指向元素会被排在p2所指向元素的后面  </span><br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> a[<span class="hljs-number">10</span>]=&#123;<span class="hljs-number">5</span>,<span class="hljs-number">9</span>,<span class="hljs-number">56</span>,<span class="hljs-number">356</span>,<span class="hljs-number">36</span>,<span class="hljs-number">6356</span>&#125;;  <br>    qsort(a,<span class="hljs-number">10</span>,<span class="hljs-keyword">sizeof</span>(<span class="hljs-type">int</span>),cmp);<br>    <span class="hljs-comment">//参数（第一个元素的指针，元素个数，元素大小，函数指针）  </span><br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;i&lt;<span class="hljs-number">10</span>;i++)&#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>,a[i]);  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">cmp</span><span class="hljs-params">(<span class="hljs-type">void</span>*p1,<span class="hljs-type">void</span>*p2)</span>  <br>&#123;  <br>    <span class="hljs-comment">//因为比较的是数字所以是强制转化为(int*)  </span><br>    <span class="hljs-keyword">return</span>(*(<span class="hljs-type">int</span>*)p1-*(<span class="hljs-type">int</span>*)p2);  <br>&#125;<span class="hljs-comment">//如果文件后缀名为cpp将函数声明中的void改为const void</span><br></code></pre></td></tr></table></figure>
<hr />
<h2 id="归并排序">归并排序</h2>
<p>合并数组：取两组中 最小/最大
的数进行比较，并依照大小先后放入结果数组。</p>
<p>实现：对于一个无序数组a[N] <span class="math display">\[
a[1],a[2],a[3] \dots a[N]
\]</span> 第一次：先处理 a[1],a[2] 形成一个有序数组 a[1-2] ，再处理
a[3],a[4] 形成一个有序数组 a[3-4] ，将 a[1-2] 和 a[3-4] 合并为 a[1-4]
<span class="math display">\[
a[1-2],a[3-4],a[5] \dots a[N]
\]</span> <span class="math display">\[
a[1-4],a[5],a[6] \dots a[N]
\]</span> 第二次：同样的方法将 a[5] , a[6] , a[7] , a[8] 处理为有序数组
a[5-8] ,将 a[1-4] 和 a[5-8] 合并为 a[1-8] <span class="math display">\[
a[1-4],a[5-8],a[9] \dots a[N]
\]</span> <span class="math display">\[
a[1-8],a[9],a[10] \dots a[N]
\]</span> 第三次：同样的方法将 a[9] , a[10] , a[11] , a[12] , a[13] ,
a[14] , a[15] , a[16] 处理为有序数组 a[9-16] ,将 a[1-8] 和 a[9-16]合并为
a[1-16] <span class="math display">\[
a[1-8],a[9-16],a[17] \dots a[N]
\]</span> <span class="math display">\[
a[1-16],a[17],a[18] \dots a[N]
\]</span> <span class="math display">\[
\dots
\]</span> 最后结果得到一个有序的数组。</p>
<p>这里提供作者 大卫·加勒斯 的merge sort<a
target="_blank" rel="noopener" href="https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html">动态图地址</a></p>
<p>(合并数组)代码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-type">void</span> <span class="hljs-title function_">merge</span><span class="hljs-params">(<span class="hljs-type">int</span> n,<span class="hljs-type">int</span>*a,<span class="hljs-type">int</span> m,<span class="hljs-type">int</span>*b,<span class="hljs-type">int</span>*c)</span>;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> n,m;  <br>    <span class="hljs-type">int</span> a[<span class="hljs-number">100</span>],b[<span class="hljs-number">100</span>],c[<span class="hljs-number">200</span>];<span class="hljs-comment">//c数组为结果数组  </span><br>    <span class="hljs-comment">//数组1</span><br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;n);  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;i&lt;n;i++)&#123;  <br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;a[i]);  <br>    &#125;  <br>    <span class="hljs-comment">//数组2</span><br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;m);  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;i&lt;m;i++)&#123;  <br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;b[i]);  <br>    &#125;  <br>    merge(n,a,m,b,c);  <span class="hljs-comment">//函数实现</span><br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;i&lt;n+m;i++)&#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>,c[i]);  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">merge</span><span class="hljs-params">(<span class="hljs-type">int</span> n,<span class="hljs-type">int</span> *a,<span class="hljs-type">int</span> m,<span class="hljs-type">int</span> *b,<span class="hljs-type">int</span> *c)</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> i=<span class="hljs-number">0</span>,j=<span class="hljs-number">0</span>,index=<span class="hljs-number">0</span>;  <br>    <span class="hljs-keyword">while</span>(i&lt;n&amp;&amp;j&lt;m)&#123;  <br>        <span class="hljs-keyword">if</span>(a[i]&lt;b[j])&#123;  <br>            c[index]=a[i];  <br>            i++;  <br>        &#125;  <br>        <span class="hljs-keyword">else</span>&#123;  <br>            c[index]=b[j];  <br>            j++;  <br>        &#125;  <br>        index++;  <br>    &#125;  <br>    <span class="hljs-keyword">if</span>(i==n)&#123;  <br>        <span class="hljs-keyword">for</span>(;j&lt;m;j++)&#123;  <br>            c[index]=b[j];  <br>            index++;  <br>        &#125;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span>&#123;  <br>        <span class="hljs-keyword">for</span>(;i&lt;n;i++)&#123;  <br>            c[index]=a[i];  <br>            index++;  <br>        &#125;  <br>    &#125;  <br>&#125;<br></code></pre></td></tr></table></figure>
<p>归并排序完整代码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;time.h&gt;</span></span><br><span class="hljs-type">int</span> arr[<span class="hljs-number">10000001</span>];<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span><br>&#123;<br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++) &#123;<br>		srand(i);<br>		arr[i] = rand() % <span class="hljs-number">10000</span>;<br>	&#125;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">Merge</span><span class="hljs-params">(<span class="hljs-type">int</span> arr[], <span class="hljs-type">int</span> tmp[], <span class="hljs-type">int</span> start, <span class="hljs-type">int</span> mid, <span class="hljs-type">int</span> end)</span><span class="hljs-comment">//合并操作</span><br>&#123;<br>	<span class="hljs-type">int</span> i = start;<span class="hljs-comment">//i为左小组的第一个元素位置</span><br>	<span class="hljs-type">int</span> j = mid + <span class="hljs-number">1</span>;<span class="hljs-comment">//j为右小组的第一个元素位置</span><br>	<span class="hljs-type">int</span> k = start;<span class="hljs-comment">//tmp数组当前的起始位置</span><br>	<span class="hljs-keyword">while</span> (i &lt; mid + <span class="hljs-number">1</span> &amp;&amp; j &lt; end + <span class="hljs-number">1</span>)<span class="hljs-comment">//左小组越界或右小组越界才能退出</span><br>	&#123;<br>		<span class="hljs-keyword">if</span> (arr[i] &lt;= arr[j])<br>		&#123;<br>			tmp[k++] = arr[i++];<br>		&#125;<br>		<span class="hljs-keyword">else</span><br>		&#123;<br>			tmp[k++] = arr[j++];<br>		&#125;<br>	&#125;<br>	<span class="hljs-keyword">while</span> (j &lt; end + <span class="hljs-number">1</span>)<span class="hljs-comment">//如果右边小组没有越界</span><br>	&#123;<br>		tmp[k++] = arr[j++];<br>	&#125;<br>	<span class="hljs-keyword">while</span> (i &lt; mid + <span class="hljs-number">1</span>)<span class="hljs-comment">//如果左边小组没有越界</span><br>	&#123;<br>		tmp[k++] = arr[i++];<br>	&#125;<br>	<span class="hljs-keyword">for</span> (i = start; i &lt;= end; i++)<br>	&#123;<br>		arr[i] = tmp[i];<br>	&#125;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">MergeS</span><span class="hljs-params">(<span class="hljs-type">int</span> arr[], <span class="hljs-type">int</span> tmp[], <span class="hljs-type">int</span> start, <span class="hljs-type">int</span> end)</span><br>&#123;<br>	<span class="hljs-keyword">if</span> (start &lt; end)<br>	&#123;<br>		<span class="hljs-type">int</span> mid = (start + end) / <span class="hljs-number">2</span>;<br>		<span class="hljs-comment">//通过递归实现算法</span><br>		MergeS(arr, tmp, start, mid);<br>		MergeS(arr, tmp, mid + <span class="hljs-number">1</span>, end);<br>		Merge(arr, tmp, start, mid, end);<br>	&#125;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">MergeSort</span><span class="hljs-params">(<span class="hljs-type">int</span> arr[], <span class="hljs-type">int</span> len)</span><br>&#123;<br>	<span class="hljs-comment">//开了一个排序后结果保存的临时数组</span><br>	<span class="hljs-type">int</span>* tmp = (<span class="hljs-type">int</span>*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(<span class="hljs-type">int</span>) * len);<br>	MergeS(arr, tmp, <span class="hljs-number">0</span>, len - <span class="hljs-number">1</span>);<br>	<span class="hljs-built_in">free</span>(tmp);<br>&#125;<br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span><br>&#123;<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;参与排序的个数：&quot;</span>);<br>	<span class="hljs-type">int</span> n;<br>	<span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;n);<br>	random(n);<br>	<span class="hljs-type">double</span> start, finish;<br>	start = (<span class="hljs-type">double</span>)clock();<br>	MergeSort(arr, n);<br>	<span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++)<br>	&#123;<br>		<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d  &quot;</span>, arr[i]);<br>	&#125;<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;\n&quot;</span>);<br>	finish = (<span class="hljs-type">double</span>)clock();<br>	<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%lf ms&quot;</span>, finish - start);<span class="hljs-comment">//运行时间</span><br>	<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<hr />
<h2 id="堆排序">堆排序</h2>
<p>这种排序可以看作是一种选择排序的优化.。</p>
<p>实现：首先要形成树，这里从最低的根部逐个向上对树进行生成。第一次维护后的树只能保证树的根为最大的数，因此将最大的数转移到树维护范围的最后一个数据（最后一个数的正确位置），并且将此时的最后一个数据移出树的维护范围，一直到将所有数都放到正确的位置，达到排序的效果。</p>
<p>假设数据如图：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="原数据" />
<figcaption aria-hidden="true">原数据</figcaption>
</figure>
<p>第一次操作（生成树）:</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="第一次操作" />
<figcaption aria-hidden="true">第一次操作</figcaption>
</figure>
<p>第二次操作：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="第二次操作" />
<figcaption aria-hidden="true">第二次操作</figcaption>
</figure>
<p><span class="math display">\[
\dots
\]</span></p>
<p>最后结果：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="结果" />
<figcaption aria-hidden="true">结果</figcaption>
</figure>
<p>代码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdlib.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;time.h&gt;</span></span><br><span class="hljs-type">int</span> arr[<span class="hljs-number">100000006</span>];<br><span class="hljs-type">void</span> <span class="hljs-title function_">random</span><span class="hljs-params">(<span class="hljs-type">int</span> n)</span><br>&#123;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= n; i++) &#123;<br>        arr[i - <span class="hljs-number">1</span>] = rand() % <span class="hljs-number">10000000</span>;<br>    &#125;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">weihu</span><span class="hljs-params">(<span class="hljs-type">int</span>* arr, <span class="hljs-type">int</span> start, <span class="hljs-type">int</span> end)</span>;<br><span class="hljs-type">void</span> <span class="hljs-title function_">dui</span><span class="hljs-params">(<span class="hljs-type">int</span>* arr, <span class="hljs-type">int</span> len)</span>;<br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span><br>&#123;<br>    <br>    <span class="hljs-type">int</span> n;<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;参与排序的个数：&quot;</span>);<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>, &amp;n);<br>    random(n);<br>    <span class="hljs-type">double</span> start, finish;<br>    start = (<span class="hljs-type">double</span>)clock();<br>    dui(arr, n);<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++) &#123;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>, arr[i]);<br>    &#125;<br>    finish = (<span class="hljs-type">double</span>)clock();<br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%lf ms&quot;</span>, finish - start);<span class="hljs-comment">//运行时间</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">weihu</span><span class="hljs-params">(<span class="hljs-type">int</span>* arr, <span class="hljs-type">int</span> start, <span class="hljs-type">int</span> end)</span><br>&#123;<br>    <span class="hljs-type">int</span> tmp = arr[start];<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = start*<span class="hljs-number">2</span>+<span class="hljs-number">1</span>; i &lt;= end; i = i * <span class="hljs-number">2</span> + <span class="hljs-number">1</span>) &#123;<span class="hljs-comment">//向左侧延申的树,从上往下维护  </span><br>        <span class="hljs-keyword">if</span> (i &lt; end &amp;&amp; arr[i] &lt; arr[i + <span class="hljs-number">1</span>]) &#123;<br>            i++;<span class="hljs-comment">//i标记为左右孩子的最大值  </span><br>        &#125;<br>        <span class="hljs-keyword">if</span> (arr[i] &gt; tmp) &#123;<span class="hljs-comment">//tmp没有变，维护的是增加的顶部数字和右侧数字  </span><br>            arr[start] = arr[i];<br>            start = i;<br>        &#125;<br>        <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-keyword">break</span>;<span class="hljs-comment">//如果一个有序后面的都是有序的  </span><br>        &#125;<br>    &#125;<br>    arr[start] = tmp;<span class="hljs-comment">//填回数字  </span><br>&#125;<br><span class="hljs-type">void</span> <span class="hljs-title function_">dui</span><span class="hljs-params">(<span class="hljs-type">int</span>* arr, <span class="hljs-type">int</span> len)</span><br>&#123;<br>    <span class="hljs-comment">//从下往上形成树</span><br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = len / <span class="hljs-number">2</span> - <span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i--) &#123;<span class="hljs-comment">//维护的结点都是下面有孩子的数据  </span><br>        weihu(arr, i, len - <span class="hljs-number">1</span>);<br>    &#125;<br>    <span class="hljs-type">int</span> tmp;<br>    <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">0</span>; i &lt; len - <span class="hljs-number">1</span>; i++) &#123;<br>        tmp = arr[<span class="hljs-number">0</span>];<br>        arr[<span class="hljs-number">0</span>] = arr[len - <span class="hljs-number">1</span> - i];<br>        arr[len - <span class="hljs-number">1</span> - i] = tmp;<br>        weihu(arr, <span class="hljs-number">0</span>, len - <span class="hljs-number">1</span> - i - <span class="hljs-number">1</span>);<span class="hljs-comment">//变化后维护（最后一个数据不维护）  </span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<hr />
<h2 id="希尔排序">希尔排序</h2>
<p>这种排序可以看作是插入排序的优化。</p>
<p>那为什么说希尔排序是针对插入排序的一种优化呢？
我们都知道，插入排序在两种情况下工作量最小</p>
<ol type="1">
<li><p>在大多数元素基本有序的情况下。<br />
元素基本有序的话，元素之间也就不需要频繁地交换，工作量自然减少</p></li>
<li><p>在元素数量比较少的情况下。<br />
元素数量n变小，时间复杂度O(n^2)自然也变小。</p></li>
</ol>
<p>作者观点：希尔排序相较于传统的插入排序在一定程度上减少了数据交换次数，提高了效率。</p>
<p>下面是原理图解。</p>
<p>第一次操作：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="第一次" />
<figcaption aria-hidden="true">第一次</figcaption>
</figure>
<p>第二次操作：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="第二次" />
<figcaption aria-hidden="true">第二次</figcaption>
</figure>
<p>第三次操作：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="第三次" />
<figcaption aria-hidden="true">第三次</figcaption>
</figure>
<p>最终结果：</p>
<figure>
<img
src="" srcset="/img/loading.gif" lazyload
alt="结果" />
<figcaption aria-hidden="true">结果</figcaption>
</figure>
<p>代码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-type">void</span> <span class="hljs-title function_">xiersort</span><span class="hljs-params">(<span class="hljs-type">int</span>*arr,<span class="hljs-type">int</span> length)</span>;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> arr[<span class="hljs-number">100</span>];  <br>    <span class="hljs-type">int</span> n;  <br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;n);  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;i&lt;n;i++)&#123;  <br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;arr[i]);  <br>    &#125;  <br>    xiersort(arr,n);  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;i&lt;n;i++)&#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d &quot;</span>,arr[i]);  <br>    &#125;  <br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">xiersort</span><span class="hljs-params">(<span class="hljs-type">int</span>*arr,<span class="hljs-type">int</span> length)</span> &#123;  <br>   <span class="hljs-type">int</span> h = <span class="hljs-number">1</span>;  <br>    <span class="hljs-keyword">while</span> (h &lt; length / <span class="hljs-number">3</span>) &#123; <span class="hljs-comment">//此处的&#x27;3&#x27;是可改变的 </span><br>        h = <span class="hljs-number">3</span> * h + <span class="hljs-number">1</span>;<span class="hljs-comment">//为了最后“h/3”的值等于1  </span><br>    &#125;  <br>    <span class="hljs-keyword">while</span> (h &gt;= <span class="hljs-number">1</span>)&#123;  <span class="hljs-comment">//从尾部开始，前方的元素依此插入</span><br>        <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=h;i&lt;length;i++)&#123;<br>	        <span class="hljs-comment">//对相互间隔h个数的数组进行插入排序  </span><br>            <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> j=i;j&gt;=h &amp;&amp; arr[j]&lt;arr[j-h];j-=h)&#123;<br>                <span class="hljs-type">int</span> temp=arr[j];arr[j]=arr[j-h];arr[j-h]=temp;  <br>            &#125;  <br>        &#125;  <br>        h=h/<span class="hljs-number">3</span>;  <br>    &#125;  <br>&#125;<br></code></pre></td></tr></table></figure>
<hr />

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E7%AE%97%E6%B3%95%E4%B8%93%E6%A0%8F/" class="category-chain-item">算法专栏</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E7%AE%97%E6%B3%95%E5%AD%A6%E4%B9%A0/" class="print-no-link">#算法学习</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>排序算法</div>
      <div>http://example.com/posts/61237/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>晓寒</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年11月12日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-cc-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/posts/47591/" title="背包DP">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">背包DP</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments">
      
  <script type="text/javascript">
    Fluid.utils.loadComments('#comments', function() {
      var light = 'github-light';
      var dark = 'github-dark';
      var schema = document.documentElement.getAttribute('data-user-color-scheme');
      if (schema === 'dark') {
        schema = dark;
      } else {
        schema = light;
      }
      window.UtterancesThemeLight = light;
      window.UtterancesThemeDark = dark;
      var s = document.createElement('script');
      s.setAttribute('src', 'https://utteranc.es/client.js');
      s.setAttribute('repo', 'Dion6850/Dion6850.github.io');
      s.setAttribute('issue-term', 'title');
      
      s.setAttribute('theme', schema);
      s.setAttribute('crossorigin', 'anonymous');
      document.getElementById('comments').appendChild(s);
    })
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  


  
  









    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> <div style="font-size: 0.85rem"> <span id="timeDate">载入天数...</span> <span id="times">载入时分秒...</span> <script src="/css/time.js"></script> </div> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    

  

</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


    <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
    <script>
        (function (window, document) {
            var typing = Fluid.plugins.typing;
            var subtitle = document.getElementById('subtitle');
            if (!subtitle || !typing) {
                return;
            }
            var text = subtitle.getAttribute('data-typed-text');
            
            typing(text);
            
        })(window, document);
    </script>
 


  
    
      <script  src="/js/img-lazyload.js" ></script>
    
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/5.0.0/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  
      <script>
        if (!window.MathJax) {
          window.MathJax = {
            tex    : {
              inlineMath: { '[+]': [['$', '$']] }
            },
            loader : {
              load: ['ui/lazy']
            },
            options: {
              renderActions: {
                insertedScript: [200, () => {
                  document.querySelectorAll('mjx-container').forEach(node => {
                    let target = node.parentNode;
                    if (target.nodeName.toLowerCase() === 'li') {
                      target.parentNode.classList.add('has-jax');
                    }
                  });
                }, '', false]
              }
            }
          };
        } else {
          MathJax.startup.document.state(0);
          MathJax.texReset();
          MathJax.typeset();
          MathJax.typesetPromise();
        }

        Fluid.events.registerRefreshCallback(function() {
          if ('MathJax' in window && MathJax.startup.document && typeof MathJax.startup.document.state === 'function') {
            MathJax.startup.document.state(0);
            MathJax.texReset();
            MathJax.typeset();
            MathJax.typesetPromise();
          }
        });
      </script>
    

  <script  src="https://lib.baomitu.com/mathjax/3.2.2/es5/tex-mml-chtml.js" ></script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="/css/time.js"></script>
<script src="/css/huadong.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<!-- hexo injector body_end start --><script src="/js/backgroundize.js"></script><!-- hexo injector body_end end --></body>
</html>
